import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  JoinColumn,
  ManyToOne,
} from 'typeorm';
import { IsNotEmpty } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { RestaurantEntity } from '@/entity/restaurant.entity';
import { UserEntity } from '@/entity/user.entity';

@Entity()
export class OrderEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ comment: '用户ID' })
  userId: number;

  @ManyToOne(() => UserEntity)
  @JoinColumn({ name: 'userId' })
  user: UserEntity;

  @Column({ comment: '餐厅ID' })
  restaurantId: number;

  @ManyToOne(() => RestaurantEntity)
  @JoinColumn({ name: 'restaurantId' })
  restaurant: RestaurantEntity;

  @Column({ comment: '订单状态：0-未支付，1-已支付，2-正在制作，3-已上菜' })
  status: string;

  @ApiProperty({ description: '订单编号' })
  @Column({ type: 'varchar', comment: '订单编号', unique: true })
  @IsNotEmpty({ message: '订单编号不能为空' })
  orderNo: string;

  @Column({ type: 'varchar', comment: '订单备注', nullable: true })
  remark: string;

  @Column({ comment: '订单总金额' })
  totalAmount: number;

  @Column({ comment: '订单创建时间', default: () => 'CURRENT_TIMESTAMP' })
  createTime: Date;

  @Column({
    comment: '订单更新时间',
    default: () => 'CURRENT_TIMESTAMP',
    onUpdate: 'CURRENT_TIMESTAMP',
  })
  updateTime: Date;

  @Column({
    comment: '订单菜品列表',
    type: 'json',
    nullable: false,
  })
  dishList: string;
}
